﻿using B.S.BasicData.Domain.TSH;
using B.S.BasicData.ErrorCode;
using B.S.BasicData.Infrastructure.Interfaces;
using B.S.BasicData.Write.Api.Application.Command.TSH;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace B.S.BasicData.Write.Api.Application.CommandHandler.TSH
{
    /// <summary>
    /// 在线箱子命令处理层      ---tsh
    /// </summary>
    public class QueryOnlineBoxesCommandHandler : IRequestHandler<QueryOnlineBoxesCommand, ApiResult<List<OnlineBoxes>>>
    {
        private readonly IBaseRepository<OnlineBoxes> _repository;

        public QueryOnlineBoxesCommandHandler(IBaseRepository<OnlineBoxes> repository)
        {
            _repository = repository;
        }

        public async Task<ApiResult<List<OnlineBoxes>>> Handle(QueryOnlineBoxesCommand request, CancellationToken cancellationToken)
        {
            var query = _repository.GetAll().ToList();

            //if (!string.IsNullOrEmpty(request.WarehouseName))
            //{
            //    query = query.Where(x => x.WarehouseName.Contains(request.WarehouseName));
            //}

            //if (!string.IsNullOrEmpty(request.BoxNumber))
            //{
            //    query = query.Where(x => x.BoxNumber.Contains(request.BoxNumber));
            //}

            return new ApiResult<List<OnlineBoxes>> { Code = ApiEnums.Success, Msg = "查询成功", Data = query.ToList()};
        }
    }

    /// <summary>
    /// 添加在线箱子命令处理程序
    /// </summary>
    public class CreateOnlineBoxCommandHandler : IRequestHandler<CreateOnlineBoxCommand, ApiResult<int>>
    {
        private readonly IBaseRepository<OnlineBoxes> _repository;

        public CreateOnlineBoxCommandHandler(IBaseRepository<OnlineBoxes> repository)
        {
            _repository = repository;
        }

        public async Task<ApiResult<int>> Handle(CreateOnlineBoxCommand request, CancellationToken cancellationToken)
        {
            var onlineBox = new OnlineBoxes
            {
                WarehouseName = request.WarehouseName,
                BoxNumber = request.BoxNumber,
                Weight = request.Weight,
                Length = request.Length,
                Width = request.Width,
                Height = request.Height,
                States = request.States,
                Remarks = request.Remarks
            };

            var result = _repository.Add(onlineBox);
            return new ApiResult<int> { Code = ApiEnums.Success, Msg = "添加成功", Data = result };
        }
    }

    /// <summary>
    /// 更新在线箱子命令处理程序
    /// </summary>
    public class UpdateOnlineBoxCommandHandler : IRequestHandler<UpdateOnlineBoxCommand, ApiResult<int>>
    {
        private readonly IBaseRepository<OnlineBoxes> _repository;

        public UpdateOnlineBoxCommandHandler(IBaseRepository<OnlineBoxes> repository)
        {
            _repository = repository;
        }

        public async Task<ApiResult<int>> Handle(UpdateOnlineBoxCommand request, CancellationToken cancellationToken)
        {
            var onlineBox = _repository.GetById(request.Id);
            if (onlineBox == null)
            {
                return new ApiResult<int> { Code = ApiEnums.Error, Msg = "在线箱子不存在" };
            }

            onlineBox.WarehouseName = request.WarehouseName;
            onlineBox.BoxNumber = request.BoxNumber;
            onlineBox.Weight = request.Weight;
            onlineBox.Length = request.Length;
            onlineBox.Width = request.Width;
            onlineBox.Height = request.Height;
            onlineBox.States = request.States;
            onlineBox.Remarks = request.Remarks;

            var result = _repository.Update(onlineBox);
            return new ApiResult<int> { Code = ApiEnums.Success, Msg = "更新成功", Data = result };
        }
    }

    /// <summary>
    /// 删除在线箱子命令处理程序
    /// </summary>
    public class DeleteOnlineBoxCommandHandler : IRequestHandler<DeleteOnlineBoxCommand, ApiResult<int>>
    {
        private readonly IBaseRepository<OnlineBoxes> _repository;

        public DeleteOnlineBoxCommandHandler(IBaseRepository<OnlineBoxes> repository)
        {
            _repository = repository;
        }

        public async Task<ApiResult<int>> Handle(DeleteOnlineBoxCommand request, CancellationToken cancellationToken)
        {
            var onlineBox = _repository.GetById(request.Id);
            if (onlineBox == null)
            {
                return new ApiResult<int> { Code = ApiEnums.Error, Msg = "在线箱子不存在" };
            }

            var result = _repository.Delete(onlineBox);
            return new ApiResult<int> { Code = ApiEnums.Success, Msg = "删除成功", Data = result };
        }
    }
}
